<?php

    require_once dirname(__FILE__) . '/config/config.php';
    require_once dirname(__FILE__) . '/config/connection.php';
    require_once dirname(__FILE__) . '/libraries/lib_date.php';

    class dao_news
    {
        public function __construct()
        {

        }

        public function get_list_news()
        {
            $con = new connection();
            $db  = $con->open_connect();

            $query  = "SELECT * FROM tbl_news";
            $result = mysqli_query($db, $query);
            $list   = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }

            $con->close_connect();

            return $list;
        }

        public function get_list_cat()
        {
            $con = new connection();
            $db  = $con->open_connect();

            $query  = "SELECT * FROM tbl_category";
            $result = mysqli_query($db, $query);
            $list   = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }

            $con->close_connect();

            return $list;
        }

        public function add_news($title, $content, $cat_id, $status_id)
        {
            $con = new connection();
            $db  = $con->open_connect();

            $cat = $this->find_cat($cat_id);

            $query = "INSERT INTO tbl_news(title, content, cat_id, cat_name, status_id, created_date) VALUES ('$title','$content', $cat_id,'" . $cat['name'] . "',$status_id,'" . lib_date::get_now() . "')";
            if (mysqli_query($db, $query)) {
                $con->close_connect();

                return TRUE;
            } else {
                $con->close_connect();

                return FALSE;
            }
        }

        public function add_category($name)
        {
            $con = new connection();
            $db  = $con->open_connect();

            $query = "INSERT INTO tbl_category(name) VALUES ('" . $name . "')";
            if (mysqli_query($db, $query)) {
                $con->close_connect();

                return TRUE;
            } else {
                $con->close_connect();

                return FALSE;
            }
        }

        public function update_news($id, $title, $content, $cat_id, $status_id)
        {
            $con = new connection();
            $db  = $con->open_connect();

            $cat = $this->find_cat($cat_id);

            $query = "UPDATE tbl_news SET title = '" . $title . "', content = '" . $content . "', cat_id = " . $cat_id . ", cat_name = '" . $cat['name'] . "', status_id = $status_id, modified_date = '" . lib_date::get_now() . "' WHERE id = " . $id;
            if (mysqli_query($db, $query)) {
                $con->close_connect();

                return TRUE;
            } else {
                $con->close_connect();

                return FALSE;
            }
        }

        public function update_category($id, $name)
        {
            $con = new connection();
            $db  = $con->open_connect();

            $query = "UPDATE tbl_category SET name = '" . $name . "' WHERE id = " . $id;
            if (mysqli_query($db, $query)) {
                $con->close_connect();

                return TRUE;
            } else {
                $con->close_connect();

                return FALSE;
            }
        }

        public function delete_news($id)
        {
            $con = new connection();
            $db  = $con->open_connect();

            $query = "DELETE FROM tbl_news WHERE id = " . $id;
            if (mysqli_query($db, $query)) {
                $con->close_connect();

                return TRUE;
            } else {
                $con->close_connect();

                return FALSE;
            }
        }

        public function delete_category($id)
        {
            $con = new connection();
            $db  = $con->open_connect();

            $query = "DELETE FROM tbl_category WHERE id = " . $id . ")";
            if (mysqli_query($db, $query)) {
                $con->close_connect();

                return TRUE;
            } else {
                $con->close_connect();

                return FALSE;
            }
        }

        public function find_news($id)
        {
            $con = new connection();
            $db  = $con->open_connect();

            $query  = "SELECT * FROM tbl_news WHERE id = " . $id;
            $result = mysqli_query($db, $query);
            $row    = mysqli_fetch_array($result);

            $con->close_connect();

            return $row;
        }

        public function find_news_by($criteria, $value)
        {
            $con = new connection();
            $db  = $con->open_connect();

            if ($criteria == 'id' || $criteria == 'cat_id') {
                $query = "SELECT * FROM tbl_news WHERE $criteria = $value";
            } else {
                $query = "SELECT * FROM tbl_news WHERE $criteria LIKE '%$value%'";
            }
            $list   = array();
            $result = mysqli_query($db, $query);
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }

            $con->close_connect();

            return $list;
        }

        public function find_cat($id)
        {
            $con = new connection();
            $db  = $con->open_connect();

            $query  = "SELECT * FROM tbl_category WHERE id = $id";
            $result = mysqli_query($db, $query);
            $row    = mysqli_fetch_array($result);

            $con->close_connect();

            return $row;
        }

        public function get_list_status()
        {
            $con = new connection();
            $db  = $con->open_connect();

            $query  = "SELECT * FROM tbl_status";
            $list   = array();
            $result = mysqli_query($db, $query);
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }

            $con->close_connect();

            return $list;
        }

        public function find_status($id){
            $con = new connection();
            $db  = $con->open_connect();

            $query  = "SELECT * FROM tbl_status WHERE id = $id";
            $result = mysqli_query($db, $query);
            $row    = mysqli_fetch_array($result);

            $con->close_connect();

            return $row;
        }
    }